<?php

namespace app\api\controller;

use app\api\service\ImageService;
use app\api\service\SystemService;
use app\api\validate\CommonValidate;
use app\api\validate\ImageValidate;
use think\File;

class Common extends Base
{
    protected $middleware = ['NotFoundMiddleware'];

    /**
     * 获取配置图片
     * @needLogin FALSE
     * @method GET
     * @return void
     */
    public function getImagesList()
    {
        if (($msg = ImageValidate::getImagesList($this->params)) !== true) apiReturn(201, $msg);
        list($code, $msg, $data) = ImageService::getImagesList($this->params);
        apiReturn($code, $msg, $data);
    }

    /**
     * 图片上传
     * @needLogin FALSE
     * @method POST
     * @return void
     */
    public function uploadImage()
    {
        try {
            /**@var $file File */
            $file = request()->file('file');
            list($code, $msg, $data) = SystemService::putFile($file, 'image', 'image');
            if ($code === 200) apiReturn($code, $msg, $data);
            throw new \Exception($msg);
        } catch (\Exception $exception) {
            apiReturn(201, $exception->getMessage());
        }
    }

    /**
     * 获取协议
     * @needLogin FALSE
     * @method GET
     * @return void
     */
    public function getAgreement()
    {
        if (isset($this->_global_config[$this->params['type'] ?? 'privacy_agreement']))
            $agreement = $this->_global_config[$this->params['type'] ?? 'privacy_agreement'];
        apiReturn(200, '操作成功', ['content' => $agreement ?? '']);
    }
}